package service.impl;

import entity.AcademyEntity;
import entity.SpecialtyEntity;
import dao.SpecialtyDao;
import dao.impl.SpecialtyDaoImpl;
import service.AcademyService;
import service.SpecialtyService;
import util.UUIDUtil;
import vo.GetSpecialtyAndMaxCodeVO;
import vo.GetSpecialtyVO;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SpecialtyServiceImpl implements SpecialtyService {
    private final SpecialtyDao specialtyDao = new SpecialtyDaoImpl();
    private final AcademyService academyService = new AcademyServiceImpl();

    @Override
    public List<GetSpecialtyVO> getSpecialtyInfo(String name) {
        List<Map<String, Object>> mapList = specialtyDao.getSpecialtyInfo(name);
        List<SpecialtyEntity> specialtyEntityList = SpecialtyEntity.mapListToEntityList(mapList);
        return specialtyEntityList.stream().map(specialty -> {
            GetSpecialtyVO vo = new GetSpecialtyVO();
            AcademyEntity academyEntity = academyService.getAcademyInfoById(specialty.getAcademyId());
            vo.setId(specialty.getId());
            vo.setAcademyName(academyEntity.getAcademyName());
            vo.setSpecialtyName(specialty.getSpecialtyName());
            vo.setSpecialtyCode(specialty.getSpecialtyCode());
            vo.setAcademyId(academyEntity.getId());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public int addSpecialtyInfo(SpecialtyEntity specialty) {
        specialty.setId(UUIDUtil.getUUID());
        return specialtyDao.addSpecialtyInfo(specialty);
    }

    @Override
    public int updateSpecialtyInfo(SpecialtyEntity specialty) {
        return specialtyDao.updateSpecialtyInfo(specialty);
    }

    @Override
    public int deleteSpecialty(String specialtyId) {
        return specialtyDao.deleteSpecialty(specialtyId);
    }

    @Override
    public int batchDeleteSpecialty(List<String> ids) {
        return specialtyDao.batchDeleteSpecialty(ids);
    }

    @Override
    public SpecialtyEntity getSpecialtyInfoById(String id) {
        return SpecialtyEntity.mapToEntity(specialtyDao.getSpecialtyInfoById(id));
    }

    @Override
    public List<GetSpecialtyAndMaxCodeVO> getSpecialtyByAcademyId(String academyId) {
        List<SpecialtyEntity> specialtyEntityList = SpecialtyEntity.mapListToEntityList(specialtyDao.getSpecialtyByAcademyId(academyId));
        return specialtyEntityList.stream().map(specialty -> {
            GetSpecialtyAndMaxCodeVO vo = new GetSpecialtyAndMaxCodeVO();
            vo.setId(specialty.getId());
            vo.setSpecialtyName(specialty.getSpecialtyName());
            vo.setSpecialtyCode(specialty.getSpecialtyCode());
            String maxCode = specialtyDao.getMaxClassCodeBySpecialtyId(specialty.getId());
            vo.setMaxClassCode(maxCode);
            return vo;
        }).collect(Collectors.toList());
    }
}
